extends StateBase


var dir = 1
var enemy01:Enemy01
var enemyAnimations:AnimationPlayer

func enterState(ownerNode):
	dir = 1
	var randNumber = randf_range(0.0, 1.0)
	if randNumber <= 0.5:
		dir = -1
	$SearchDurationTimer.wait_time = randf_range(0.6, 1.0)
	$SearchDurationTimer.start()
	
	
func exitState(ownerNode):
	pass
	
	
func process(delta, ownerNode):
	pass
	
	
func physicsProcess(delta, ownerNode):
	if not enemy01:
		enemy01 = ownerNode as Enemy01
		enemyAnimations = enemy01.animations as AnimationPlayer 
	
	enemyAnimations.play("walk")
	enemy01.velocity.y += enemy01.gravity * enemy01.gravityScale * delta
	
	var player = get_tree().get_first_node_in_group("Player") as PlayerScript
	var distance2Player = player.global_position.distance_to(enemy01.global_position)
	
	enemy01.enemySpriteRoot.scale.x = dir
	enemy01.velocity.x = move_toward(enemy01.velocity.x, enemy01.SPEED * dir, enemy01.acceleration * delta)
	enemy01.move_and_slide()
		
	if distance2Player <= enemy01.CHASE_DISTANCE:
		stateMachine.changeState("StateChase", ownerNode)
		$SearchDurationTimer.stop()
	elif $SearchDurationTimer.time_left <= Global.EPSILON:  
		stateMachine.changeState("StateIdle", ownerNode)
		


